﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAD_SiS.Models;
using System.Web.Security;

namespace SAD_SiS.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Details(string id)
        {
            int pid = Convert.ToInt32(id);
            using (SADEntities dbContext = new SADEntities())
            {
                var product = (from p in dbContext.V_Product where p.ProductID == pid && p.Active == true select p).SingleOrDefault();
                if (product == null)
                {
                    return View("Error");
                }
                string username = getUser();
                bool blnRate = true;
                string strRateMess = "Đánh giá cho điểm sản phẩm";
                if (username.Equals("guest"))
                {
                    blnRate = false;
                    strRateMess = "Đăng nhập để cho điểm sản phẩm";
                }
                else
                {

                    var rated = (from c in dbContext.Ratings
                                 where c.ProductID == pid && c.Username == username
                                 select c).SingleOrDefault();
                    if (rated != null)
                    {
                        blnRate = false;
                        strRateMess = "Bạn đã chấm " + rated.Score + " điểm";
                    }
                }
                ViewBag.Rate = blnRate;
                ViewBag.RateMess = strRateMess;
                return View(product);
            }

        }


        public ActionResult Rating(int productID, double score)
        {
            string username = getUser();
            if (username == null)
            {
                return Json(new { Success = false, Message = "Bạn chưa đăng nhập" });
            }
            using (var dbContext = new SADEntities())
            {
                var rating = new Rating();
                rating.RateDate = DateTime.Now;
                rating.ProductID = productID;
                rating.Username = username;
                rating.Score = score;
                dbContext.Ratings.Add(rating);
                dbContext.SaveChanges();
            }

            return Json(new { Success = true, Message = "Bạn đã chấm " + score + " điểm" });
        }

        #region helper
        private string getUser()
        {
            if (Session["username"] != null)
            {
                return Session["username"].ToString();
            }
            else
            {

                //HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                //if (authCookie == null)
                //{
                //    return "guest";
                //}
                //FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
                //return ticket.Name == null ? "guest" : ticket.Name;
                return "guest";
            }
        }
        #endregion
    }
}
